package com.zhongwang.business.module.baseCostManage.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.dao.InputImportCostDao;
import com.zhongwang.business.module.baseCostManage.model.InputMachiningModel;
import com.zhongwang.business.module.baseCostManage.model.VwEquModel;
import com.zhongwang.business.util.ShiroUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  机加成本数据导入 listener
 * @author kxx
 * @date 2020年10月19日10:15
 */
public class InputMachiningListener extends AnalysisEventListener<InputMachiningModel> {


    private InputImportCostDao inputImportCostDao;
    private ImportCallBack callBack;
    private List<InputMachiningModel> inputMachiningModel = new ArrayList<>();
    private List<VwEquModel> equ;
    private List<Integer> exceptionList = new ArrayList<>();
    private int i = 2;
    private InputMachiningModel inputMachining;

    public InputMachiningListener(InputMachiningModel inputMachining, InputImportCostDao inputImportCostDao,
                                    ImportCallBack callBack,
                                    List<VwEquModel> equ) {
        this.inputImportCostDao = inputImportCostDao;
        this.callBack = callBack;
        this.equ = equ;
        this.inputMachining = inputMachining;
    }

    @Override
    public void invoke(InputMachiningModel data, AnalysisContext context) {

        VwEquModel equipment = getName(data.getMacUnitType());
        data.setKind(inputMachining.getInputKind());
        data.setPartName(inputMachining.getInputPartName());
        if (data.getDiagramCode() == null || data.getDiagramCode().trim().equals("")) {
            data.setReason(data.getReason() + "图号没填");
        }
        if (data.getDiagramName() == null || data.getDiagramName().trim().equals("")) {
            data.setReason(data.getReason() + "零件名称没填");
        }
        if (data.getMacNum() == null) {
            data.setReason(data.getReason() + "零件数量没填");
        } else if (data.getMacNum() <= 0) {
            data.setReason(data.getReason() + "零件数量必须大于0");
        }
        if (data.getMacProcess() == null || data.getMacProcess().trim().equals("")) {
            data.setReason(data.getReason() + "加工工序没填");
        }
        if (data.getMacDeviceType() == null || data.getMacDeviceType().trim().equals("")) {
            data.setReason(data.getReason() + "设备类型没填");
        }

        if (data.getMacUnitType() == null || data.getMacUnitType().trim().equals("")) {
            data.setReason(data.getReason() + "设备型号没填");
        } else if (getName(data.getMacUnitType()) == null){
            data.setReason(data.getReason() + "设备型号不存在");
        }

        if (data.getMacHour() == null) {
            data.setReason(data.getReason() + "零件数量没填");
        } else if (data.getMacHour() <= 0) {
            data.setReason(data.getReason() + "零件数量必须大于0");
        }


        if (inputImportCostDao.selectRepetition(data) > 0) {
            data.setReason(data.getReason() + "重复数据");
        }
         /**判断并输出验证结果*/
        if (data.getReason()!= null && data.getReason().length() > 0) {
            inputMachiningModel.add(data);
            i++;
            return;
        }
        User user = ShiroUtils.getUser();
        data.setUploadUser(user.getName());
        data.setUploadDatetime(new Date());
        data.setFkId(inputMachining.getKbId());

        data.setDesignType(inputMachining.getInputDesignType());
        data.setFkInputId(inputMachining.getInputMacId());

        data.setCostKind(inputMachining.getInputCostKind());
        data.setEquCode(equipment.getCode());
        data.setMacReceiveFlag(0);
        data.setMacDeleteFlag(0);
        inputImportCostDao.insert(data);
        i++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (exceptionList.size() > 0) {
            InputMachiningModel model = new InputMachiningModel();
            model.setDiagramName("第" + exceptionList.toString() + "行数据格式不正确");
            inputMachiningModel.add(model);
        }
        if (inputMachiningModel.size() > 0) {
            callBack.onWarning(inputMachiningModel);
        } else {
            callBack.onSuccess();
        }
    }
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exception.printStackTrace();
        String err = exception.getMessage();
        exceptionList.add(i);
        i++;
        return;
    }

    private VwEquModel getName(String equTypes) {
        for (VwEquModel equipment : equ) {
            if (equipment.getName() != null && equipment.getName().equals(equTypes)) {
                return equipment;
            }
        }
        return null;
    }

}
